<?php
namespace information\service;

/**--tmpl-use--**/
use herosphp\core\Loader;

/**--tmpl-import--**/

class AdService extends CommonService{

    private $informationAdDao = null;
    private $informationAdCateDao = null;
    private $informationAdPlaceDao = null;
    private $adminManagerDao = null;
    /**--tmpl-construct--**/

    /**-- daoConstruct --**/
    public function __construct($informationAdModel, $informationAdCateModel,$informationAdPlaceDao,$admin_managers) {

        $this->setModelDao(Loader::model($informationAdModel));
        $this->informationAdCateDao = Loader::model($informationAdCateModel);
        $this->informationAdPlaceDao = Loader::model($informationAdPlaceDao);
        $this->adminManagerDao = Loader::model($admin_managers);
    }

    /**-- daoGetItem --**/
    public function getItem($conditions, $fields, $order)
    {
        if(!$order['addtime'])
        {
            //默认是时间倒叙
            $order['addtime'] = -1;
        }
        $item = parent::getItem($conditions, $fields, $order);
        if(!$item) return NULL;

        /**-- foreignitem --**/
        $information_ad_place = $this->informationAdPlaceDao->getItem($item['ad_place_key']);

        $item['information_ad_place_title'] = $information_ad_place['title'];
        $item['information_ad_place_summary'] = $information_ad_place['summary'];
        $item['information_ad_place_width'] = $information_ad_place['width'];
        $item['information_ad_place_height'] = $information_ad_place['height'];
        $item['information_ad_place_cate'] = $information_ad_place['cate'];
        $item['information_ad_place_default_type'] = $information_ad_place['default_type'];
        $item['information_ad_place_default_content'] = $information_ad_place['default_content'];
        $item['information_ad_place_status'] = $information_ad_place['status'];
        $item['information_ad_place_manager_id'] = $information_ad_place['manager_id'];
        $item['information_ad_place_app_id'] = $information_ad_place['app_id'];
        /**-- foreignitem --**/
        $admin_manager = $this->adminManagerDao->getItem($item['manager_id']);

        $item['admin_manager_username'] = $admin_manager['username'];
        $item['admin_manager_password'] = $admin_manager['password'];
        $item['admin_manager_mobile'] = $admin_manager['mobile'];
        $item['admin_manager_jobno'] = $admin_manager['jobno'];
        $item['admin_manager_email'] = $admin_manager['email'];
        $item['admin_manager_name'] = $admin_manager['name'];
        $item['admin_manager_role_id'] = $admin_manager['role_id'];
        $item['admin_manager_last_login_time'] = $admin_manager['last_login_time'];
        $item['admin_manager_last_login_ip'] = $admin_manager['last_login_ip'];
        $item['admin_manager_status'] = $admin_manager['status'];
        $item['admin_manager_addtime'] = $admin_manager['addtime'];
        $item['admin_manager_updatetime'] = $admin_manager['updatetime'];
        $item['admin_manager_app_id'] = $admin_manager['app_id'];

        //----------------------
        /**--tmpl-daoassoctodo--**/
        return $item;
    }

    /**-- daoGetItems --**/
    public function getItems($conditions, $fields, $order, $limit, $group, $having)
    {
        //按照id倒叙查询数据
        if(!$order['id']){
            $order['id'] = -1;
        }
        $items = parent::getItems($conditions, $fields, $order, $limit, $group, $having);
        if(!$items) return NULL;

        /**-- foreigncondi --**/
        $information_ad_placecondi = array();
        foreach($items as $k => $v)
        {
            if($v['ad_place_key'])
            {
                $information_ad_placecondi[] = $v['ad_place_key'];
            }
        }

        $information_ad_places = $this->informationAdPlaceDao->getItems(array('key' => array( '$in' => $information_ad_placecondi)));
        /**-- foreigncondi --**/
        $admin_managercondi = array();
        foreach($items as $k => $v)
        {
            if($v['manager_id'])
            {
                $admin_managercondi[] = $v['manager_id'];
            }
        }

        $admin_managers = $this->adminManagerDao->getItems(array('id' => array( '$in' => $admin_managercondi)));

        /**-- foreignforeach  --**/
        foreach ($items as $k => $v)
        {
            $endtime = strtotime($v['endtime']);
            $addtime = strtotime($v['addtime']);
            if($endtime<$addtime){$items[$k]['remaining_time'] = "永久";}
            else{
                $temptime  = ($endtime - $addtime) / 86400;
                $items[$k]['remaining_time'] = ceil($temptime)."天";
            }
            /**-- foreigneach --**/
            foreach($information_ad_places as $kk => $vv)
            {
                if($vv['key'] == $v['ad_place_key'])
                {
                    /**--tmpl-fieldfill--**/

                    $items[$k]['information_ad_place_title'] = $vv['title'];
                    $items[$k]['information_ad_place_summary'] = $vv['summary'];
                    $items[$k]['information_ad_place_width'] = $vv['width'];
                    $items[$k]['information_ad_place_height'] = $vv['height'];
                    $items[$k]['information_ad_place_cate'] = $vv['cate'];
                    $items[$k]['information_ad_place_default_type'] = $vv['default_type'];
                    $items[$k]['information_ad_place_default_content'] = $vv['default_content'];
                    $items[$k]['information_ad_place_status'] = $vv['status'];
                    $items[$k]['information_ad_place_manager_id'] = $vv['manager_id'];
                    $items[$k]['information_ad_place_app_id'] = $vv['app_id'];
                }
            }

            /**-- foreigneach --**/
            foreach($admin_managers as $kk => $vv)
            {
                if($vv['id'] == $v['manager_id'])
                {
                    /**--tmpl-fieldfill--**/

                    $items[$k]['admin_manager_username'] = $vv['username'];
                    $items[$k]['admin_manager_password'] = $vv['password'];
                    $items[$k]['admin_manager_mobile'] = $vv['mobile'];
                    $items[$k]['admin_manager_jobno'] = $vv['jobno'];
                    $items[$k]['admin_manager_email'] = $vv['email'];
                    $items[$k]['admin_manager_name'] = $vv['name'];
                    $items[$k]['admin_manager_role_id'] = $vv['role_id'];
                    $items[$k]['admin_manager_last_login_time'] = $vv['last_login_time'];
                    $items[$k]['admin_manager_last_login_ip'] = $vv['last_login_ip'];
                    $items[$k]['admin_manager_status'] = $vv['status'];
                    $items[$k]['admin_manager_addtime'] = $vv['addtime'];
                    $items[$k]['admin_manager_updatetime'] = $vv['updatetime'];
                    $items[$k]['admin_manager_app_id'] = $vv['app_id'];

                }
            }
        }
        return $items;
    }
}

